//
//  RSMessageListViewController.swift
//  ChatDemo
//
//  Created by 范强伟 on 2024/5/10.
//

import UIKit

class RSMessageListViewController: UIViewController {

    var dataList = Array<RSMessageModel>()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.title = "聊天列表"
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        view.backgroundColor = .white
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "新建", style: .done, target: self, action: #selector(createNew))
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        getHistoryData()
    }
    
    @objc func createNew(){
        let nav =  RSChatViewController(roomId: "\(milliStamp)")
        self.navigationController?.pushViewController(nav, animated: true)
    }
    
    // 读取聊天记录
    func getHistoryData(){
        dataList.removeAll()
        RSMessageViewModel.getAllMessage()?.forEach({ model in
            dataList.append(model)
        })
        tableView.reloadData()
    }
    
    
    lazy var tableView: UITableView = {
        let table = UITableView(frame: .zero, style: .plain)
        table.backgroundColor = .clear
        table.separatorStyle = .none;
        table.delegate = self
        table.dataSource = self
        table.bounces = false
        table.showsVerticalScrollIndicator = false
        table.contentInsetAdjustmentBehavior = .never
        table.sectionHeaderHeight = 0
        table.sectionFooterHeight = 0
        table.isUserInteractionEnabled = true
        table.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")
//        table.register(UITableViewCell.Type, forCellReuseIdentifier: "UITableViewCell")
        return table
    }()

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension RSMessageListViewController: UITableViewDelegate, UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell")!
        let item = dataList[indexPath.row]
        cell.textLabel?.text = item.roomId
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      return  dataList.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let item = dataList[indexPath.row]
        let vc = RSChatViewController(roomId: item.roomId)
        navigationController?.pushViewController(vc, animated: true)
    }
    
}
